

//
//  HMEmotionKeyboard.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMEmotionKeyboard.h"
//上部表情图标视图
#import "HMEmotionListView.h"
//下部自定义toolbar
#import "HMEmotionToolbar.h"
//
#import "HMEmotion.h"


@interface HMEmotionKeyboard()<HMEmotionToolbarDelegate>

//HMEmotionListView
@property (nonatomic, strong) HMEmotionListView *listView;
//HMEmotionToolbar
@property (nonatomic, strong) HMEmotionToolbar *toolbar;
//HMEmotion 单个表情
//@property (nonatomic, strong) HMEmotion *emotion;

//默认表情图标 数组
@property (nonatomic, strong) NSArray *defaultEmotions;
//Emoji图标 数组
@property (nonatomic, strong) NSArray *emojiEmotions;
//浪小花图标 数组
@property (nonatomic, strong) NSArray *lxhEmotions;


@end


@implementation HMEmotionKeyboard

//懒加载创建数组
//默认图标
- (NSArray *)defaultEmotions{
    if (!_defaultEmotions) {
        NSString *plist = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/default/info.plist" ofType:nil];
        _defaultEmotions = [HMEmotion objectArrayWithFile:plist];
        
        //设置单个表情的所在文件夹路径(下面两个方法都可以)
        //方法一:让self.emojiEmotions 里面的每一个表情都调用setDirectory:方法,并且把 @"EmotionIcons/default" 传进去
        [self.defaultEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/default"];
        
         //方法二
//        for (HMEmotion *emotion in self.defaultEmotions) {
//            emotion.directory = @"EmotionIcons/default";
//        }
    }
    return _defaultEmotions;
}

//Emoji图标
- (NSArray *)emojiEmotions{
    if (!_emojiEmotions) {
        NSString *plist = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/emoji/info.plist" ofType:nil];
        _emojiEmotions = [HMEmotion objectArrayWithFile:plist];
        
        [self.emojiEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/emoji"];
    }
    return _emojiEmotions;
}


//浪小花图标
- (NSArray *)lxhEmotions{
    if (!_lxhEmotions) {
        NSString *plist = [[NSBundle mainBundle] pathForResource:@"EmotionIcons/lxh/info.plist" ofType:nil];
        _lxhEmotions = [HMEmotion objectArrayWithFile:plist];
        
        [self.lxhEmotions makeObjectsPerformSelector:@selector(setDirectory:) withObject:@"EmotionIcons/lxh"];
    }
    return _lxhEmotions;
}



+ (instancetype)keyboard{

    return  [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //1.添加 HMEmotionListView
        HMEmotionListView *listView = [[HMEmotionListView alloc] init];
        
        [self addSubview:listView];
        self.listView = listView;
        
        //2.添加 HMEmotionToolbar
        HMEmotionToolbar *toolbar = [[HMEmotionToolbar alloc] init];
        toolbar.delegate = self;
        [self addSubview:toolbar];
        self.toolbar = toolbar;
        
    }
    return self;
}






//设置frame
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //1.下部 toolbar
    CGFloat toolbarW = kScreenWidth;
    CGFloat toolbarH = 44;
    CGFloat toolbarX = 0;
    CGFloat toolbarY = self.height - toolbarH;
    
    self.toolbar.frame = CGRectMake(toolbarX, toolbarY, toolbarW, toolbarH);
    
    //2.上部 listView
    CGFloat listViewW = kScreenWidth;
    CGFloat listViewH = self.height - toolbarH;
    CGFloat listViewX = 0;
    CGFloat listViewY = 0;

    self.listView.frame = CGRectMake(listViewX, listViewY, listViewW, listViewH);
    

}

#pragma mark --- HMEmotionToolbarDelegate 监听 toolbar上按钮的点击
- (void)emotionToolbar:(HMEmotionToolbar *)toolbar didSeletedButton:(HMEmotionType)emotionType{

    switch (emotionType) {
        case HMEmotionTypeRecent://最近
        {
            HMLog(@"最近");
        }
            break;
        case HMEmotionTypeDefault://默认
        {
            HMLog(@"默认");
            self.listView.emotions = self.defaultEmotions;
            HMLog(@"默认:%ld",[self.listView.emotions count]);
            
        }
            break;
        case HMEmotionTypeEmoji://Emoji
        {
            HMLog(@"Emoji");
            self.listView.emotions = self.emojiEmotions;
            HMLog(@"Emoji:%ld",[self.listView.emotions count]);
        }
            break;
        case HMEmotionTypeLxh://浪小花
        {
            HMLog(@"浪小花");
            self.listView.emotions = self.lxhEmotions;
            HMLog(@"浪小花:%ld",[self.listView.emotions count]);
        }
            break;
    
            
        default:
            break;
    }

}



@end
